<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="enableEdit" :disabled="enable"
        >编辑</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="saveEdit" :disabled="!enable"
        >保存</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="useDefaultValue"
        >套用默认值</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="mappingList" @selection-change="handleSelectionChange">
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="描述" align="center" prop="description" />
      <el-table-column label="默认" align="center" prop="defaultValue" />
      <el-table-column label="当前值" align="center" prop="parameterValue" >
        <template slot-scope="scope">
          <el-input v-model="scope.row.parameterValue" :disabled="!enable"></el-input>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改运行机器人参数对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="值" prop="parameterValue">
          <el-input v-model="form.parameterValue" placeholder="请输入值" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMapping, getMapping, delMapping, addMapping, updateMapping, exportMapping, saveMappingList } from "@/api/platform/mapping";

export default {
  name: "Mapping",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 运行机器人参数表格数据
      mappingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      enable:false,
      runningId:undefined,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        parameterValue: undefined,
        runningId: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.runningId = this.$route.query['runningId']
    this.getList();
  },
  methods: {
    /** 查询运行机器人参数列表 */
    getList() {
      this.loading = true;
      this.queryParams.runningId = this.runningId
      listMapping(this.queryParams).then(response => {
        this.mappingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    enableEdit(){
      this.enable=!this.enable
    },
    saveEdit(){
      // this.enable=!this.enable
      this.saveParameters()
    },
    useDefaultValue(){
      this.mappingList.forEach(v=>{
        v.parameterValue=v.defaultValue;
      })
      this.saveParameters()
    },
    saveParameters(){
       let query = {runningId:this.runningId}
       this.loading=true
       saveMappingList(this.mappingList,query).then(res=>{
         this.enable=false
         this.getList()
       })
    },
    // 表单重置
    reset() {
      this.form = {
        runningId: undefined,
        parameterId: undefined,
        parameterValue: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.runningId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加运行机器人参数";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const runningId = row.runningId || this.ids
      getMapping(runningId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改运行机器人参数";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.runningId != undefined) {
            updateMapping(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addMapping(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const runningIds = row.runningId || this.ids;
      this.$confirm('是否确认删除运行机器人参数编号为"' + runningIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMapping(runningIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有运行机器人参数数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportMapping(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
